﻿using Lhxm.Sgs.DalFactory;
using Lhxm.Sgs.IDal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lhxm.Sgs.Bll
{
    public abstract class BaseService<T>where T:class,new()
    {
        /// <summary>
        /// 如何BLL都是通过DBSession访问数据子类
        /// </summary>
        /// 
        public IDBSession CurrentDBSession
        {
            get
            {
                return DBSessionFactory.CreateDBSesion();//temp
            }
        }
        public IDal.IBaseDal<T> CurrentDal { get; set; }
        public abstract void SetCurrentDal();
        public BaseService()
        {
            SetCurrentDal();//保证子类一定要实现的抽象方法(多态)
        }
        public IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda)
        {
            return CurrentDal.LoadEntities(whereLambda);
        }

        public IQueryable<T> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, s>> orderByLambda, bool isAsc)
        {
            return CurrentDal.LoadPageEntities<s>(pageIndex,pageSize,out totalCount,whereLambda,orderByLambda,isAsc);
        }
        public bool DeleteEntiy(T entity)
        {
            CurrentDal.DeleteEntiy(entity);
            return CurrentDBSession.SaveChanges();
        }
        public bool EditEntity(T entity)
        {
            CurrentDal.EditEntity(entity);
            return CurrentDBSession.SaveChanges();
        }

        public T AddEntity(T entity)
        {
            CurrentDal.AddEntity(entity);
            CurrentDBSession.SaveChanges();
            return entity;
        }
    }
}
